package club.banyuan;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    private static List<Vehicle> vehicles = load();

    private static final int MENU_ADD = 1;
    private static final int MENU_REQUIRE = 2;
    private static final int MENU_RENT = 3;
    private static final int MENU_RETURN = 4;
    private static final int MENU_QUIT = 0;
    private static final int MENU = 5;

    private static final Scanner scanner = new Scanner(System.in);
    private static int flag = MENU;

    public static void main(String[] args) {
        System.out.println("欢迎使用半圆租车系统");
        while (true) {
            // 分⽀选择流程
            switch (flag) {
                case MENU:
                    menuPrint();
                    break;
                case MENU_ADD:
                    // 添加⻋辆
                    addVehicle();
                    save(vehicles);
                    break;
                case MENU_REQUIRE:
                    // 打印车辆信息
                    printInfo();
                    break;
                case MENU_RENT:
                    // 出租车辆
                    rentVehicle();
                    break;
                case MENU_RETURN:
                    // 归还车辆
                    returnVehicle();
                    break;
                case MENU_QUIT: // 退出
                    System.out.println("谢谢使用，再见!");
                    return;
                default:
                    break;
            }
        }
    }

    private static List<Vehicle> load() {
        try (FileReader fileReader = new FileReader(
                "/Users/edz/banyuan/Java/CarRentalSystem/CarRentalSystemAns/vehicles.json");
             BufferedReader reader = new BufferedReader(fileReader);
        ) {
            StringBuilder builder = new StringBuilder();
            String line = reader.readLine();
            while (line != null) {
                builder.append(line);
                line = reader.readLine();
            }

            return JSONObject.parseArray(builder.toString(), Vehicle.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    private static void save(List<Vehicle> vehicles) {
        try (FileWriter fileWriter = new FileWriter(
                "/Users/edz/banyuan/Java/CarRentalSystem/CarRentalSystemAns/vehicles.json");
             BufferedWriter writer = new BufferedWriter(fileWriter);
        )
        {
            writer.write(JSONObject.toJSONString(vehicles, SerializerFeature.WriteClassName));
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    // 添加车辆
    private static void addVehicle() {
        System.out.println("添加车辆");
//        Scanner sc = new Scanner(System.in);
        System.out.println("请输入车辆年份（2005-2020）：");

        check();

        int year = scanner.nextInt();
        while (year < 2005 || year > 2020) {
            System.out.println("输入年份不合法，请重新输入：");
            year = scanner.nextInt();
        }
        System.out.println("请输入车辆厂家:");
        String factory = scanner.next();

        while (true) {
            System.out.println("请输入要添加的车辆类型：1. 客车 2. 货车");
            check();
            int chosen = scanner.nextInt();
            switch (chosen) {
                case 1:
                    System.out.println("请输入客车载客量");
                    check();
                    int passengers = scanner.nextInt();
                    Vehicle bus = new Bus(year, factory, false, passengers);
                    vehicles.add(bus);
                    System.out.println("添加成功");
                    System.out.println(bus);
                    break;
                case 2:
                    System.out.println("请输入货车载货量(吨)");
                    double capacity = scanner.nextDouble();
                    Vehicle truck = new Truck(year, factory, false, capacity);
                    vehicles.add(truck);
                    System.out.println("添加成功");
                    System.out.println(truck);
                    break;
                default:
                    System.out.println("输入不合法，请重新输入：");
                    break;
            }
            if (chosen == 1 || chosen == 2) {
                break;
            }
        }
        flag = MENU;
    }

    // 查询车辆
    private static void printInfo() {
        System.out.println("查询车辆");
        for (int i = 0; i < vehicles.size(); i++) {
            if (vehicles.get(i) == null)
                break;
            System.out.println((i + 1) + "." + vehicles.get(i));
        }
        flag = MENU;
    }

    // 出租车辆
    private static void rentVehicle() {
        System.out.println("出租车辆");
        System.out.println("请选择租用的车辆编号(0返回上一级):");
        for (int i = 0; i < vehicles.size(); i++) {
            if (!vehicles.get(i).isRent() && vehicles.get(i) != null) {
                System.out.println((i + 1) + "." + vehicles.get(i));
            }
        }
        check();
        int choose = scanner.nextInt();
        if (choose == 0) {
        } else if (vehicles.get(choose - 1).isRent()) {
            System.out.println("操作失败，该车辆已出租");
        } else if (!vehicles.get(choose - 1).isRent()) {
            System.out.println("操作成功！");
            vehicles.get(choose - 1).setRent(true);
            System.out.println(vehicles.get(choose - 1));
        } else {
            System.out.println("输入错误！");
        }

        flag = MENU;
    }

    // 归还车辆
    private static void returnVehicle() {
        System.out.println("归还车辆");
        System.out.println("请选择租用的车辆编号(0返回上一级):");
        for (int i = 0; i < vehicles.size(); i++) {
            if (vehicles.get(i).isRent() && vehicles.get(i) != null) {
                System.out.println((i + 1) + "." + vehicles.get(i));
            }
        }
        check();
        int choose = scanner.nextInt();
        if (choose == 0) {
        } else if (!vehicles.get(choose - 1).isRent()) {
            System.out.println("操作失败，该车辆未出租");
        } else if (vehicles.get(choose - 1).isRent()) {
            System.out.println("操作成功！");
            vehicles.get(choose - 1).setRent(false);
            System.out.println(vehicles.get(choose - 1));
        } else {
            System.out.println("输入错误！");
        }
        flag = MENU;
    }

    private static void menuPrint() {

        System.out.println("================");
        System.out.println("1. 添加⻋辆");
        System.out.println("2. 查询⻋辆");
        System.out.println("3. 出租⻋辆");
        System.out.println("4. 归还⻋辆");
        System.out.println("0. 退出系统");
        check();
        int input = scanner.nextInt();
        switch (input) {
            case 1:
                flag = MENU_ADD;
                break;
            case 2:
                flag = MENU_REQUIRE;
                break;
            case 3:
                flag = MENU_RENT;
                break;
            case 4:
                flag = MENU_RETURN;
                break;
            case 0:
                flag = MENU_QUIT;
                break;
            default:
                System.out.println("输入不合法，请重新输入");
                break;
        }
    }

    // 判断输入是否为数字
    public static void check() {
        while (!scanner.hasNextInt()) {
            System.out.println("输入错误，请重新输入");
            scanner.next();
        }
    }
}